﻿namespace WMS6.Web.Billing
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Text;
    using System.Threading;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoDetailServiceRef;
    using WMS6.Web.BaseInfoServiceRef;

    public class Contract : BasePage
    {
        protected Button btnAddDetail;
        protected Button btnAudit;
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnDeleteDetail;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnRevertAudit;
        protected Button btnSave;
        protected CheckBox chkIsActive;
        protected CheckBox chkIsAudit;
        protected DropDownList ddlContractType;
        protected DropDownList ddlDepartmentID;
        protected DropDownList ddlManageType;
        protected DropDownList ddlSettleAccount;
        protected HtmlAnchor DownloadAttach;
        protected HtmlForm F9011;
        protected GridView gvContract;
        protected HtmlInputHidden hdnAttachID;
        protected HtmlInputHidden hdnAttachLineID;
        protected HtmlInputHidden hdncalendarFlag;
        protected HtmlInputHidden hdnContractID;
        protected HtmlInputHidden hdnCopyFlag;
        protected HtmlInputHidden hdnDataFormat;
        protected HtmlInputHidden hdnfileName;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnfullPath;
        protected HtmlInputHidden hdnImgName;
        protected HtmlInputHidden hdnImgpath;
        protected HtmlInputHidden hdnLineID;
        protected HtmlInputHidden hdnResponse;
        protected HtmlHead Head1;
        protected HtmlImage imgOwnerID;
        protected Label Label1;
        protected Label Label2;
        protected Label Label4;
        protected Label Label5;
        protected Label Label6;
        protected Label Label7;
        protected Label Label8;
        protected Label lblContractID;
        protected Label lblContractType;
        protected Label lblCost;
        protected Label lblCostTypes;
        protected Label lblDepartmentID;
        protected Label lblDeposit;
        protected Label lblDescr;
        protected Label lblEndTime;
        protected Label lblGeneralLedger;
        protected Label lblManageType;
        protected Label lblMsg;
        protected Label lblOverdueRent;
        protected Label lblOwner;
        protected Label lblOwnerID;
        protected Label lblPayment;
        protected Label lblRemark;
        protected Label lblSettleAccount;
        protected Label lblStartTime;
        protected Label lblTaxGroup;
        protected Label lblWhLoad;
        protected HtmlLink mycss;
        protected Label one1;
        protected Label one2;
        protected Label one3;
        protected Label one4;
        protected HtmlAnchor SaveAttach;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAllBillsLowestCharge;
        protected TextBox txtAllBillsLowestGenralLedger;
        protected TextBox txtAllBillsLowestTagGroup;
        protected TextBox txtContractID;
        protected TextBox txtDeposit;
        protected TextBox txtDescr;
        protected TextBox txtEndTime;
        protected TextBox txtLowestInBoundBillCharge;
        protected TextBox txtLowestInBoundBillGenralLedger;
        protected TextBox txtLowestInBoundBillTagGroup;
        protected TextBox txtLowestInBoundCharge;
        protected TextBox txtLowestInBoundGenralLedger;
        protected TextBox txtLowestInBoundTagGroup;
        protected TextBox txtLowestInitialStorageBillCharge;
        protected TextBox txtLowestInitialStorageBillGenralLedger;
        protected TextBox txtLowestInitialStorageBillTagGroup;
        protected TextBox txtLowestInitialStorageCharge;
        protected TextBox txtLowestInitialStorageGenralLedger;
        protected TextBox txtLowestInitialStorageTagGroup;
        protected TextBox txtLowestOutBoundCharge;
        protected TextBox txtLowestOutBoundGenralLedger;
        protected TextBox txtLowestOutBoundTagGroup;
        protected TextBox txtLowestRecurringStorageBillCharge;
        protected TextBox txtLowestRecurringStorageBillGenralLedger;
        protected TextBox txtLowestRecurringStorageBillTagGroup;
        protected TextBox txtOverdueRent;
        protected TextBox txtOwnerID;
        protected TextBox txtPayment;
        protected TextBox txtRemark;
        protected TextBox txtStartTime;
        protected TextBox txtWhLoad;
        protected UpdatePanel up1;
        protected HtmlGenericControl UpLoadDiv;

        protected void btnAddDetail_Click(object sender, EventArgs e)
        {
            if (!this.chkIsAudit.Checked)
            {
                if (string.IsNullOrEmpty(this.txtContractID.Text) || (this.hdnFlag.Value == ""))
                {
                    this.btnSave_Click(null, null);
                    this.hdnLineID.Value = "10";
                }
                base.Response.Redirect(string.Format("RentWhDetail.aspx?from={0}&ID={1}&LineID={2}", base.UrlEncode(this.hdnFrom.Value), base.UrlEncode(this.txtContractID.Text.Trim()), base.UrlEncode(this.hdnLineID.Value)), true);
            }
            else
            {
                this.lblMsg.Text = "审核后不能新增明细";
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,4);</script>", false);
            }
        }

        protected void btnAudit_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                try
                {
                    if (!string.IsNullOrEmpty(this.txtContractID.Text))
                    {
                        if (!this.chkIsAudit.Checked)
                        {
                            string strContractID = string.Empty;
                            strContractID = CommoneMethodServiceClientProxy.GetInstance().AuditContractByContractID(new List<string> { this.txtContractID.Text }, "Y");
                            this.SetContractInfo2Form();
                            this.lblMsg.Text = strContractID + "审核成功!";
                        }
                        else
                        {
                            this.lblMsg.Text = "已审核,无法继续审核!";
                        }
                    }
                    else
                    {
                        this.lblMsg.Text = "请保存合同后在审核!";
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
            else
            {
                this.lblMsg.Text = "没有保存无法审核";
            }
        }

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                this.txtContractID.Text = "";
                this.txtContractID.Enabled = true;
                this.chkIsAudit.Checked = false;
                this.hdnCopyFlag.Value = "1";
                this.hdnFlag.Value = "";
            }
            else
            {
                this.lblMsg.Text = "没有保存无法复制";
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                if (!this.chkIsAudit.Checked)
                {
                    if (!string.IsNullOrEmpty(this.txtContractID.Text.Trim()))
                    {
                        try
                        {
                            List<string> ContractIDS = new List<string> {
                                this.txtContractID.Text.Trim()
                            };
                            this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(ContractIDS, "CONTRACT") + base.GetGlobalMsgResource("F9110_009");
                            this.btnCopy.Enabled = false;
                            this.btnDelete.Enabled = false;
                            this.btnSave.Enabled = false;
                            this.btnAudit.Enabled = false;
                            base.DataEntityViewState = null;
                        }
                        catch (Exception ex)
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F9110_010") + ex.Message;
                        }
                    }
                }
                else
                {
                    this.lblMsg.Text = "审核后无法删除";
                }
            }
            else
            {
                this.lblMsg.Text = "没有保存无法删除";
            }
        }

        protected void btnDeleteDetail_Click(object sender, EventArgs e)
        {
            if (!this.chkIsAudit.Checked)
            {
                try
                {
                    if (!string.IsNullOrEmpty(this.txtContractID.Text.Trim()) && (this.hdnFlag.Value == "1"))
                    {
                        List<string> contractID = base.GetSelectedIDsInGridView(this.gvContract, "ContractID");
                        List<string> lineID = base.GetSelectedIDsInGridView(this.gvContract, "LineID");
                        if (contractID.Count > 0)
                        {
                            this.lblMsg.Text = BaseInfoDetailServiceClientProxy.GetInstance().DeleteDetail(contractID, lineID, new List<string>(), "RENT_WH_DETAIL") + "删除成功!";
                            this.SetContractInfo2Form();
                        }
                        else
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
                finally
                {
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,4);</script>", false);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,4);</script>", false);
                this.lblMsg.Text = "审核后无法删除";
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("ContractQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("ContractList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("Contract.aspx?from={0}", base.UrlEncode(this.hdnFrom.Value.Trim())), false);
        }

        protected void btnRevertAudit_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                try
                {
                    if (!string.IsNullOrEmpty(this.txtContractID.Text))
                    {
                        if (this.chkIsAudit.Checked)
                        {
                            string strContractID = string.Empty;
                            strContractID = CommoneMethodServiceClientProxy.GetInstance().AuditContractByContractID(new List<string> { this.txtContractID.Text }, "N");
                            this.SetContractInfo2Form();
                            this.lblMsg.Text = strContractID + "反审核成功!";
                        }
                        else
                        {
                            this.lblMsg.Text = "合同还未审核!";
                        }
                    }
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = ex.Message;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtOwnerID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_002");
            }
            else if (string.IsNullOrEmpty(this.txtStartTime.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_003");
                this.txtStartTime.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtEndTime.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_004");
                this.txtEndTime.Focus();
            }
            else if (this.chkIsAudit.Checked)
            {
                this.lblMsg.Text = "审核后无法继续修改!";
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateContractInfo();
            }
            else
            {
                this.InsertContractInfo();
            }
        }

        protected void DownloadAttach_Click(object sender, EventArgs e)
        {
            Exception ex;
            HttpRequest _Request = this.Page.Request;
            HttpResponse _Response = this.Page.Response;
            string _fileName = base.Server.UrlDecode(this.hdnfileName.Value);
            string _fullPath = base.Server.UrlDecode(this.hdnfullPath.Value);
            long _speed = 0xfa000;
            try
            {
                FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
                BinaryReader br = new BinaryReader(myFile);
                try
                {
                    _Response.AddHeader("Accept-Ranges", "bytes");
                    _Response.Buffer = false;
                    long fileLength = myFile.Length;
                    long startBytes = 0;
                    double pack = 10240.0;
                    int sleep = ((int) Math.Floor((double) ((1000.0 * pack) / ((double) _speed)))) + 1;
                    if (_Request.Headers["Range"] != null)
                    {
                        _Response.StatusCode = 0xce;
                        startBytes = Convert.ToInt64(_Request.Headers["Range"].Split(new char[] { '=', '-' })[1]);
                    }
                    _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                    _Response.AddHeader("Connection", "Keep-Alive");
                    _Response.ContentType = "application/octet-stream";
                    _Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, Encoding.UTF8));
                    br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                    int maxCount = ((int) Math.Floor((double) (((double) (fileLength - startBytes)) / pack))) + 1;
                    for (int i = 0; i < maxCount; i++)
                    {
                        if (_Response.IsClientConnected)
                        {
                            _Response.BinaryWrite(br.ReadBytes(int.Parse(pack.ToString())));
                            Thread.Sleep(sleep);
                        }
                        else
                        {
                            i = maxCount;
                        }
                    }
                }
                catch (Exception exception1)
                {
                    ex = exception1;
                    this.lblMsg.Text = ex.Message;
                }
                finally
                {
                    br.Close();
                    myFile.Close();
                }
            }
            catch (Exception exception2)
            {
                ex = exception2;
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,4);</script>", false);
            }
        }

        protected void gvContract_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F9011", this.gvContract);
        }

        protected void gvContract_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                int i = this.gvContract.PageIndex * this.gvContract.PageSize;
                e.Row.Cells[1].Text = ((e.Row.RowIndex + 1) + i).ToString();
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
            }
        }

        protected void gvContract_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                string sortDirection = string.Empty;
                if (base.GridViewSortDirection == SortDirection.Ascending)
                {
                    base.GridViewSortDirection = SortDirection.Descending;
                    sortDirection = "DESC";
                }
                else
                {
                    base.GridViewSortDirection = SortDirection.Ascending;
                    sortDirection = "ASC";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
            finally
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',2,4);</script>", false);
            }
        }

        private void InsertContractInfo()
        {
            ContractInfo contractInfo;
            string strContractID;
            Exception ex;
            if (!string.IsNullOrEmpty(this.hdnCopyFlag.Value))
            {
                try
                {
                    contractInfo = base.DataEntityViewState as ContractInfo;
                    contractInfo.ContractID = this.txtContractID.Text;
                    contractInfo.OwnerID = this.txtOwnerID.Text;
                    contractInfo.Descr = this.txtDescr.Text;
                    contractInfo.ContractType = this.ddlContractType.SelectedValue;
                    contractInfo.SettleAccount = this.ddlSettleAccount.SelectedValue;
                    if (!string.IsNullOrEmpty(this.txtPayment.Text))
                    {
                        contractInfo.Payment = decimal.Parse(this.txtPayment.Text);
                    }
                    else
                    {
                        contractInfo.Payment = 0M;
                    }
                    contractInfo.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                    contractInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                    contractInfo.WhLoad = this.txtWhLoad.Text;
                    contractInfo.ManageType = this.ddlManageType.SelectedValue;
                    if (!string.IsNullOrEmpty(this.txtDeposit.Text))
                    {
                        contractInfo.Deposit = decimal.Parse(this.txtDeposit.Text);
                    }
                    else
                    {
                        contractInfo.Deposit = 0M;
                    }
                    if (!string.IsNullOrEmpty(this.txtOverdueRent.Text))
                    {
                        contractInfo.OverdueRent = decimal.Parse(this.txtOverdueRent.Text);
                    }
                    else
                    {
                        contractInfo.OverdueRent = 0M;
                    }
                    contractInfo.DepartmentID = this.ddlDepartmentID.SelectedValue;
                    contractInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    contractInfo.IsAudit = this.chkIsAudit.Checked ? "Y" : "N";
                    contractInfo.Remark = this.txtRemark.Text;
                    contractInfo.CreatedBy = base.UserInfo.UserID;
                    if (!string.IsNullOrEmpty(this.txtLowestInBoundCharge.Text))
                    {
                        contractInfo.LowestInboundCharge = decimal.Parse(this.txtLowestInBoundCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInboundCharge = 0M;
                    }
                    contractInfo.LowestInboundTaxGroup = this.txtLowestInBoundTagGroup.Text;
                    contractInfo.LowestInboundGeneralLedger = this.txtLowestInBoundGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestOutBoundCharge.Text))
                    {
                        contractInfo.LowestOutboundCharge = decimal.Parse(this.txtLowestOutBoundCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestOutboundCharge = 0M;
                    }
                    contractInfo.LowestOutboundTaxGroup = this.txtLowestOutBoundTagGroup.Text;
                    contractInfo.LowestOutboundGeneralLedger = this.txtLowestOutBoundGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                    {
                        contractInfo.LowestInitialStorageCharge = decimal.Parse(this.txtLowestInitialStorageCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInitialStorageCharge = 0M;
                    }
                    contractInfo.LowestInitialStorageTaxGroup = this.txtLowestInitialStorageTagGroup.Text;
                    contractInfo.LowestInitialStorageGeneralLedger = this.txtLowestInitialStorageGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                    {
                        contractInfo.LowestInboundBillCharge = decimal.Parse(this.txtLowestInBoundBillCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInboundBillCharge = 0M;
                    }
                    contractInfo.LowestInboundBillTaxGroup = this.txtLowestInBoundBillTagGroup.Text;
                    contractInfo.LowestInboundBillGeneralLedger = this.txtLowestInBoundBillGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestInitialStorageBillCharge.Text))
                    {
                        contractInfo.LowestInitialStorageBillCharge = decimal.Parse(this.txtLowestInitialStorageBillCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInitialStorageBillCharge = 0M;
                    }
                    contractInfo.LowestInitialStorageBillTaxGroup = this.txtLowestInitialStorageBillTagGroup.Text;
                    contractInfo.LowestInitialStorageBillGeneralLedger = this.txtLowestInitialStorageBillGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestRecurringStorageBillCharge.Text))
                    {
                        contractInfo.LowestRecurringStorageBillCharge = decimal.Parse(this.txtLowestRecurringStorageBillCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestRecurringStorageBillCharge = 0M;
                    }
                    contractInfo.LowestRecurringStorageBillTaxGroup = this.txtLowestRecurringStorageBillTagGroup.Text;
                    contractInfo.LowestRecurringStorageBillGeneralLedger = this.txtLowestRecurringStorageBillGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtAllBillsLowestCharge.Text))
                    {
                        contractInfo.AllBillsLowestCharge = decimal.Parse(this.txtAllBillsLowestCharge.Text);
                    }
                    else
                    {
                        contractInfo.AllBillsLowestCharge = 0M;
                    }
                    contractInfo.AllBillsLowestGeneralLedger = this.txtAllBillsLowestGenralLedger.Text;
                    contractInfo.AllBillsLowestTaxGroup = this.txtAllBillsLowestTagGroup.Text;
                    strContractID = string.Empty;
                    strContractID = BaseInfoServiceClientProxy.GetInstance().Insert(contractInfo, "CONTRACT");
                    contractInfo.ContractID = strContractID;
                    this.hdnFlag.Value = "1";
                    this.hdnContractID.Value = strContractID;
                    this.SetContractInfo2Form();
                    base.DataEntityViewState = contractInfo;
                    this.txtContractID.Text = strContractID;
                    this.txtContractID.Enabled = false;
                    this.lblMsg.Text = strContractID + base.GetGlobalMsgResource("F9110_007");
                    this.hdnCopyFlag.Value = "";
                }
                catch (Exception exception1)
                {
                    ex = exception1;
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9110_008") + ex.Message;
                }
            }
            else
            {
                try
                {
                    contractInfo = new ContractInfo {
                        ContractID = this.txtContractID.Text,
                        OwnerID = this.txtOwnerID.Text,
                        Descr = this.txtDescr.Text,
                        ContractType = this.ddlContractType.SelectedValue,
                        SettleAccount = this.ddlSettleAccount.SelectedValue
                    };
                    if (!string.IsNullOrEmpty(this.txtPayment.Text))
                    {
                        contractInfo.Payment = decimal.Parse(this.txtPayment.Text);
                    }
                    else
                    {
                        contractInfo.Payment = 0M;
                    }
                    contractInfo.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                    contractInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                    contractInfo.WhLoad = this.txtWhLoad.Text;
                    contractInfo.ManageType = this.ddlManageType.SelectedValue;
                    if (!string.IsNullOrEmpty(this.txtDeposit.Text))
                    {
                        contractInfo.Deposit = decimal.Parse(this.txtDeposit.Text);
                    }
                    else
                    {
                        contractInfo.Deposit = 0M;
                    }
                    if (!string.IsNullOrEmpty(this.txtOverdueRent.Text))
                    {
                        contractInfo.OverdueRent = decimal.Parse(this.txtOverdueRent.Text);
                    }
                    else
                    {
                        contractInfo.OverdueRent = 0M;
                    }
                    contractInfo.DepartmentID = this.ddlDepartmentID.SelectedValue;
                    contractInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                    contractInfo.IsAudit = this.chkIsAudit.Checked ? "Y" : "N";
                    contractInfo.Remark = this.txtRemark.Text;
                    contractInfo.CreatedBy = base.UserInfo.UserID;
                    if (!string.IsNullOrEmpty(this.txtLowestInBoundCharge.Text))
                    {
                        contractInfo.LowestInboundCharge = decimal.Parse(this.txtLowestInBoundCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInboundCharge = 0M;
                    }
                    contractInfo.LowestInboundTaxGroup = this.txtLowestInBoundTagGroup.Text;
                    contractInfo.LowestInboundGeneralLedger = this.txtLowestInBoundGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestOutBoundCharge.Text))
                    {
                        contractInfo.LowestOutboundCharge = decimal.Parse(this.txtLowestOutBoundCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestOutboundCharge = 0M;
                    }
                    contractInfo.LowestOutboundTaxGroup = this.txtLowestOutBoundTagGroup.Text;
                    contractInfo.LowestOutboundGeneralLedger = this.txtLowestOutBoundGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                    {
                        contractInfo.LowestInitialStorageCharge = decimal.Parse(this.txtLowestInitialStorageCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInitialStorageCharge = 0M;
                    }
                    contractInfo.LowestInitialStorageTaxGroup = this.txtLowestInitialStorageTagGroup.Text;
                    contractInfo.LowestInitialStorageGeneralLedger = this.txtLowestInitialStorageGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                    {
                        contractInfo.LowestInboundBillCharge = decimal.Parse(this.txtLowestInBoundBillCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInboundBillCharge = 0M;
                    }
                    contractInfo.LowestInboundBillTaxGroup = this.txtLowestInBoundBillTagGroup.Text;
                    contractInfo.LowestInboundBillGeneralLedger = this.txtLowestInBoundBillGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestInitialStorageBillCharge.Text))
                    {
                        contractInfo.LowestInitialStorageBillCharge = decimal.Parse(this.txtLowestInitialStorageBillCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestInitialStorageBillCharge = 0M;
                    }
                    contractInfo.LowestInitialStorageBillTaxGroup = this.txtLowestInitialStorageBillTagGroup.Text;
                    contractInfo.LowestInitialStorageBillGeneralLedger = this.txtLowestInitialStorageBillGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtLowestRecurringStorageBillCharge.Text))
                    {
                        contractInfo.LowestRecurringStorageBillCharge = decimal.Parse(this.txtLowestRecurringStorageBillCharge.Text);
                    }
                    else
                    {
                        contractInfo.LowestRecurringStorageBillCharge = 0M;
                    }
                    contractInfo.LowestRecurringStorageBillTaxGroup = this.txtLowestRecurringStorageBillTagGroup.Text;
                    contractInfo.LowestRecurringStorageBillGeneralLedger = this.txtLowestRecurringStorageBillGenralLedger.Text;
                    if (!string.IsNullOrEmpty(this.txtAllBillsLowestCharge.Text))
                    {
                        contractInfo.AllBillsLowestCharge = decimal.Parse(this.txtAllBillsLowestCharge.Text);
                    }
                    else
                    {
                        contractInfo.AllBillsLowestCharge = 0M;
                    }
                    contractInfo.AllBillsLowestGeneralLedger = this.txtAllBillsLowestGenralLedger.Text;
                    contractInfo.AllBillsLowestTaxGroup = this.txtAllBillsLowestTagGroup.Text;
                    strContractID = string.Empty;
                    strContractID = BaseInfoServiceClientProxy.GetInstance().Insert(contractInfo, "CONTRACT");
                    this.hdnFlag.Value = "1";
                    this.hdnContractID.Value = strContractID;
                    this.SetContractInfo2Form();
                    base.DataEntityViewState = contractInfo;
                    this.txtContractID.Text = strContractID;
                    this.txtContractID.Enabled = false;
                    this.lblMsg.Text = strContractID + base.GetGlobalMsgResource("F9110_007");
                }
                catch (Exception exception2)
                {
                    ex = exception2;
                    this.lblMsg.Text = base.GetGlobalMsgResource("F9110_008") + ex.Message;
                }
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlContractType, "ContractType");
            base.BindData2DDLBySysCode(this.ddlSettleAccount, "SettlementMode");
            base.BindData2DDLBySysCode(this.ddlManageType, "ManageType");
            base.BindDataDDLBySourceFrom(this.ddlDepartmentID, "DEPARTMENT");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnContractID.Value = base.Request.QueryString["ID"];
                this.hdnFlag.Value = "1";
                this.txtContractID.Enabled = false;
                this.SetContractInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["detail"]))
            {
                ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'>setTab('one',3,3);</script>", false);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        protected void SaveAttach_Click(object sender, EventArgs e)
        {
            if (this.hdnFlag.Value == "1")
            {
                if (!this.chkIsAudit.Checked)
                {
                    try
                    {
                        if (string.IsNullOrEmpty(this.hdnAttachID.Value))
                        {
                            AttachInfo attachInfo = new AttachInfo {
                                AttachID = this.hdnContractID.Value + "CRT",
                                DocID = this.hdnContractID.Value,
                                DocType = "CRT",
                                Descr = "",
                                Status = "UPLOAD",
                                Remark = "",
                                CreatedBy = base.UserInfo.UserID
                            };
                            this.hdnAttachID.Value = BaseInfoServiceClientProxy.GetInstance().Insert(attachInfo, "ATTACH");
                        }
                        string[] strUpLoad = this.hdnResponse.Value.Split(new char[] { '♣' });
                        if (strUpLoad.Length > 0)
                        {
                            WMS6.Web.BaseInfoDetailServiceRef.AttachDetailInfo attachDetailInfo = new WMS6.Web.BaseInfoDetailServiceRef.AttachDetailInfo {
                                AttachID = this.hdnAttachID.Value,
                                LineID = int.Parse(this.hdnAttachLineID.Value),
                                Status = "UPLOAD",
                                Url = strUpLoad[0],
                                AttachName = strUpLoad[1],
                                DisplayName = strUpLoad[2],
                                AttachSize = decimal.Parse(strUpLoad[3]),
                                AttachType = strUpLoad[4],
                                IsAudit = "N",
                                IsActive = "Y",
                                CreatedBy = base.UserInfo.UserID
                            };
                            if (!string.IsNullOrEmpty(BaseInfoDetailServiceClientProxy.GetInstance().InsertDetail(attachDetailInfo, "ATTACH_DETAIL")))
                            {
                                this.lblMsg.Text = "图片上传成功!";
                                this.SetContractInfo2Form();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        this.lblMsg.Text = ex.Message;
                    }
                }
                else
                {
                    this.lblMsg.Text = "审核后无法上传附件";
                }
            }
            else
            {
                this.lblMsg.Text = "没有保存无法上传附件";
            }
        }

        private void SetContractInfo2Form()
        {
            try
            {
                string strContractID = this.hdnContractID.Value;
                ContractInfo contractInfo = (ContractInfo) BaseInfoServiceClientProxy.GetInstance().GetInfoByID(strContractID, "CONTRACT");
                if (contractInfo != null)
                {
                    base.DataEntityViewState = contractInfo;
                    this.txtContractID.Text = contractInfo.ContractID;
                    this.txtOwnerID.Text = contractInfo.OwnerID;
                    this.txtDescr.Text = contractInfo.Descr;
                    this.ddlContractType.SelectedValue = contractInfo.ContractType;
                    this.ddlSettleAccount.SelectedValue = contractInfo.SettleAccount;
                    this.txtPayment.Text = base.GetPreferenceDigit(contractInfo.Payment);
                    this.txtStartTime.Text = base.GetPreferenceShortDate(contractInfo.StartTime);
                    this.txtEndTime.Text = base.GetPreferenceShortDate(contractInfo.EndTime);
                    this.txtWhLoad.Text = contractInfo.WhLoad;
                    this.ddlManageType.SelectedValue = contractInfo.ManageType;
                    this.txtOverdueRent.Text = base.GetPreferenceDigit(contractInfo.OverdueRent);
                    this.txtDeposit.Text = base.GetPreferenceDigit(contractInfo.Deposit);
                    this.ddlDepartmentID.SelectedValue = contractInfo.DepartmentID;
                    this.chkIsActive.Checked = contractInfo.IsActive == "Y";
                    this.chkIsAudit.Checked = contractInfo.IsAudit == "Y";
                    this.txtRemark.Text = contractInfo.Remark;
                    this.txtLowestInBoundCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInboundCharge);
                    this.txtLowestInBoundTagGroup.Text = contractInfo.LowestInboundTaxGroup;
                    this.txtLowestInBoundGenralLedger.Text = contractInfo.LowestInboundGeneralLedger;
                    this.txtLowestOutBoundCharge.Text = base.GetPreferenceDigit(contractInfo.LowestOutboundCharge);
                    this.txtLowestOutBoundTagGroup.Text = contractInfo.LowestOutboundTaxGroup;
                    this.txtLowestOutBoundGenralLedger.Text = contractInfo.LowestOutboundGeneralLedger;
                    this.txtLowestInitialStorageCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInitialStorageCharge);
                    this.txtLowestInitialStorageTagGroup.Text = contractInfo.LowestInitialStorageTaxGroup;
                    this.txtLowestInitialStorageGenralLedger.Text = contractInfo.LowestInitialStorageGeneralLedger;
                    this.txtLowestInBoundBillCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInboundBillCharge);
                    this.txtLowestInBoundBillTagGroup.Text = contractInfo.LowestInboundBillTaxGroup;
                    this.txtLowestInBoundBillGenralLedger.Text = contractInfo.LowestInboundBillGeneralLedger;
                    this.txtLowestInitialStorageBillCharge.Text = base.GetPreferenceDigit(contractInfo.LowestInitialStorageBillCharge);
                    this.txtLowestInitialStorageBillTagGroup.Text = contractInfo.LowestInitialStorageBillTaxGroup;
                    this.txtLowestInitialStorageBillGenralLedger.Text = contractInfo.LowestInitialStorageBillGeneralLedger;
                    this.txtLowestRecurringStorageBillCharge.Text = base.GetPreferenceDigit(contractInfo.LowestRecurringStorageBillCharge);
                    this.txtLowestRecurringStorageBillTagGroup.Text = contractInfo.LowestRecurringStorageBillTaxGroup;
                    this.txtLowestRecurringStorageBillGenralLedger.Text = contractInfo.LowestRecurringStorageBillGeneralLedger;
                    this.txtAllBillsLowestCharge.Text = base.GetPreferenceDigit(contractInfo.AllBillsLowestCharge);
                    this.txtAllBillsLowestGenralLedger.Text = contractInfo.AllBillsLowestGeneralLedger;
                    this.txtAllBillsLowestTagGroup.Text = contractInfo.AllBillsLowestTaxGroup;
                    if ((contractInfo.RentWhDetailInfos != null) && (contractInfo.RentWhDetailInfos.Count > 0))
                    {
                        int CS00001 = contractInfo.RentWhDetailInfos[contractInfo.RentWhDetailInfos.Count - 1].LineID + 10;
                        this.hdnLineID.Value = CS00001.ToString();
                    }
                    else
                    {
                        this.hdnLineID.Value = "10";
                    }
                    if (((contractInfo.AttachInfos != null) && (contractInfo.AttachInfos.AttachDetailInfos != null)) && (contractInfo.AttachInfos.AttachDetailInfos.Count > 0))
                    {
                        string dir = base.Server.MapPath(".");
                        dir = dir.Substring(0, dir.LastIndexOf(@"\"));
                        StringBuilder strHtml = new StringBuilder();
                        foreach (WMS6.Web.BaseInfoServiceRef.AttachDetailInfo info in contractInfo.AttachInfos.AttachDetailInfos)
                        {
                            strHtml.Append(" <a onclick=\"fileDownload('" + base.Server.UrlEncode(info.DisplayName + "." + info.AttachType) + "','" + base.Server.UrlEncode(dir + "/" + info.Url) + "')\"><table width=180px border=1 cellpadding=0 cellspacing=0 class=slotbox  style=\"float: left; margin-right: 5px; margin-top: 5px;\">");
                            strHtml.AppendFormat(" <tr><td colspan=2><img src=\"{0}\" width=\"180px\" height=\"160px\"> </td></tr>", "../Upload/Thumbnail/" + info.AttachName);
                            strHtml.AppendFormat(" <tr> <td width=20% nowrap>名称： </td>  <td class=areamessage>{0}</td>", info.DisplayName);
                            strHtml.AppendFormat(" <tr> <td width=20% nowrap>类型： </td>  <td class=areamessage>{0}</td>", info.AttachType);
                            strHtml.AppendFormat(" <tr> <td width=20% nowrap>大小： </td>  <td class=areamessage>{0}字节</td></table></a>", info.AttachSize);
                        }
                        this.UpLoadDiv.InnerHtml = strHtml.ToString();
                        this.hdnAttachID.Value = contractInfo.AttachInfos.AttachID;
                        this.hdnAttachLineID.Value = (contractInfo.AttachInfos.AttachDetailInfos[contractInfo.AttachInfos.AttachDetailInfos.Count - 1].LineID + 10).ToString();
                    }
                    else
                    {
                        if (contractInfo.AttachInfos != null)
                        {
                            this.hdnAttachID.Value = contractInfo.AttachInfos.AttachID;
                        }
                        this.hdnAttachLineID.Value = "10";
                    }
                    base.BindData2GridView(this.gvContract, contractInfo.RentWhDetailInfos);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateContractInfo()
        {
            try
            {
                ContractInfo contractInfo = base.DataEntityViewState as ContractInfo;
                contractInfo.OwnerID = this.txtOwnerID.Text;
                contractInfo.Descr = this.txtDescr.Text;
                contractInfo.ContractType = this.ddlContractType.SelectedValue;
                contractInfo.SettleAccount = this.ddlSettleAccount.SelectedValue;
                if (!string.IsNullOrEmpty(this.txtPayment.Text))
                {
                    contractInfo.Payment = decimal.Parse(this.txtPayment.Text);
                }
                else
                {
                    contractInfo.Payment = 0M;
                }
                contractInfo.StartTime = Convert.ToDateTime(this.txtStartTime.Text);
                contractInfo.EndTime = Convert.ToDateTime(this.txtEndTime.Text);
                contractInfo.WhLoad = this.txtWhLoad.Text;
                contractInfo.ManageType = this.ddlManageType.SelectedValue;
                if (!string.IsNullOrEmpty(this.txtOverdueRent.Text))
                {
                    contractInfo.OverdueRent = decimal.Parse(this.txtOverdueRent.Text);
                }
                else
                {
                    contractInfo.OverdueRent = 0M;
                }
                if (!string.IsNullOrEmpty(this.txtDeposit.Text))
                {
                    contractInfo.Deposit = decimal.Parse(this.txtDeposit.Text);
                }
                else
                {
                    contractInfo.Deposit = 0M;
                }
                contractInfo.DepartmentID = this.ddlDepartmentID.SelectedValue;
                contractInfo.IsActive = this.chkIsActive.Checked ? "Y" : "N";
                contractInfo.IsAudit = this.chkIsAudit.Checked ? "Y" : "N";
                contractInfo.UpdatedBy = base.UserInfo.UserID;
                contractInfo.Remark = this.txtRemark.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInBoundCharge.Text))
                {
                    contractInfo.LowestInboundCharge = decimal.Parse(this.txtLowestInBoundCharge.Text);
                }
                else
                {
                    contractInfo.LowestInboundCharge = 0M;
                }
                contractInfo.LowestInboundTaxGroup = this.txtLowestInBoundTagGroup.Text;
                contractInfo.LowestInboundGeneralLedger = this.txtLowestInBoundGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestOutBoundCharge.Text))
                {
                    contractInfo.LowestOutboundCharge = decimal.Parse(this.txtLowestOutBoundCharge.Text);
                }
                else
                {
                    contractInfo.LowestOutboundCharge = 0M;
                }
                contractInfo.LowestOutboundTaxGroup = this.txtLowestOutBoundTagGroup.Text;
                contractInfo.LowestOutboundGeneralLedger = this.txtLowestOutBoundGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                {
                    contractInfo.LowestInitialStorageCharge = decimal.Parse(this.txtLowestInitialStorageCharge.Text);
                }
                else
                {
                    contractInfo.LowestInitialStorageCharge = 0M;
                }
                contractInfo.LowestInitialStorageTaxGroup = this.txtLowestInitialStorageTagGroup.Text;
                contractInfo.LowestInitialStorageGeneralLedger = this.txtLowestInitialStorageGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageCharge.Text))
                {
                    contractInfo.LowestInboundBillCharge = decimal.Parse(this.txtLowestInBoundBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestInboundBillCharge = 0M;
                }
                contractInfo.LowestInboundBillTaxGroup = this.txtLowestInBoundBillTagGroup.Text;
                contractInfo.LowestInboundBillGeneralLedger = this.txtLowestInBoundBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestInitialStorageBillCharge.Text))
                {
                    contractInfo.LowestInitialStorageBillCharge = decimal.Parse(this.txtLowestInitialStorageBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestInitialStorageBillCharge = 0M;
                }
                contractInfo.LowestInitialStorageBillTaxGroup = this.txtLowestInitialStorageBillTagGroup.Text;
                contractInfo.LowestInitialStorageBillGeneralLedger = this.txtLowestInitialStorageBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtLowestRecurringStorageBillCharge.Text))
                {
                    contractInfo.LowestRecurringStorageBillCharge = decimal.Parse(this.txtLowestRecurringStorageBillCharge.Text);
                }
                else
                {
                    contractInfo.LowestRecurringStorageBillCharge = 0M;
                }
                contractInfo.LowestRecurringStorageBillTaxGroup = this.txtLowestRecurringStorageBillTagGroup.Text;
                contractInfo.LowestRecurringStorageBillGeneralLedger = this.txtLowestRecurringStorageBillGenralLedger.Text;
                if (!string.IsNullOrEmpty(this.txtAllBillsLowestCharge.Text))
                {
                    contractInfo.AllBillsLowestCharge = decimal.Parse(this.txtAllBillsLowestCharge.Text);
                }
                else
                {
                    contractInfo.AllBillsLowestCharge = 0M;
                }
                contractInfo.AllBillsLowestGeneralLedger = this.txtAllBillsLowestGenralLedger.Text;
                contractInfo.AllBillsLowestTaxGroup = this.txtAllBillsLowestTagGroup.Text;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(contractInfo, "CONTRACT") + base.GetGlobalMsgResource("F9110_005");
                this.SetContractInfo2Form();
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F9110_006") + ex.Message;
            }
        }
    }
}

